package no.rodland.acc.tags;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;

import java.io.IOException;

import no.rodland.acc.db.DbRow;
import no.rodland.acc.model.Bank;
import no.rodland.acc.model.Budget;

/**
 * Created by IntelliJ IDEA.
 *
 * @Since: Jun 21, 2010 2:14:44 PM
 * @Author: fmr
 */
public class DbRowTag extends RodlandTag {

    private DbRow row;

    public DbRow getRow() {
        return row;
    }

    public void setRow(DbRow row) {
        this.row = row;
    }

    @Override
    public int doStartTag() throws JspException {
        try {
            if (row instanceof Bank) {
                writeBank((Bank) row);
            }
            else if (row instanceof Budget) {
                writeBudget((Budget) row);
            }
        }
        catch (IOException e) {
            throw new JspException("IOException caught in writing - " + e.getMessage());
        }
        return SKIP_BODY;
    }

    private void writeBank(Bank bank) throws IOException {
        JspWriter out = pageContext.getOut();
//        out.print("<td>" + bank.getId() + "</td>");
        out.print("<td>" + bank.getDate() + "</td>");
        out.print("<td>" + bank.getText() + "</td>");
        out.print("<td>" + bank.getAmount() + "</td>");
        out.print("<td>" + bank.getComment() + "</td>");
    }

    private void writeBudget(Budget row) throws IOException {
        JspWriter out = pageContext.getOut();
//        out.print("<td>" + row.getId() + "</td>");
        out.print("<td>" + row.getDate() + "</td>");
        out.print("<td>" + row.getText() + "</td>");
        out.print("<td>" + row.getAmount() + "</td>");
        out.print("<td>" + row.getComment() + "</td>");

    }
}
